// pages/comment/comment.js
var postData = require('../../data/posts-data.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    myComment: true,
    wannerTalk: false,
    isExpose: false,
    _num: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    var postDetail = postData.postList[0]
    _this.setData({
      postDetail: postDetail
    })
    var openid = wx.getStorageSync('openid')
    wx.request({
      url: 'https://shipin.lekuaiwang.com/api/user/talkme',
      data: {
        openid: openid
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        console.log(res.data.data)
        _this.setData({
          myCommentList: res.data.data
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  replyTap: function (e) {
    console.log(123)
    console.log(e.currentTarget.dataset)
    var openid = wx.getStorageSync('openid')
    var dataset = e.currentTarget.dataset
    var res_id = dataset.res_id
    var parent_id = dataset.parent_id
    var target_talk_id = dataset.target_talk_id
    var target_user_id = dataset.target_user_id
    var nickname = dataset.nickname
    this.setData({
      current_reply_data: {
        openid: openid,
        res_id: res_id,
        parent_id: parent_id,
        target_talk_id: target_talk_id,
        target_user_id: target_user_id,
        target_talk_nickname: nickname
      },
      wannerTalk: true
    })

  },
  getTalk: function (e) {
    var val = e.detail.value
    console.log(val)
    this.setData({
      val: val
    })
  },
  submitComment: function (e) {
    var _this = this
    var content = _this.data.val
    console.log(content)
    var userinfo = wx.getStorageSync('userInfo')
    console.log(userinfo)
    // var id = this.data.post_detail.id
    var current_reply_data = _this.data.current_reply_data
    // var comments = _this.data.comments_list.last
    // var sonComment = _this.data.comments_list.parent
    var openid = wx.getStorageSync('openid')
    var parData = {
      openid: current_reply_data.openid,
      res_id: current_reply_data.res_id,
      parent_id: current_reply_data.parent_id,
      target_talk_id: current_reply_data.target_talk_id,
      target_user_id: current_reply_data.target_user_id,
      content: '回复@ ' + current_reply_data.target_talk_nickname + '：' + content
    }
    console.log(openid)

    // if (current_reply_data) {
    //   parData = {
    //     openid: current_reply_data.openid,
    //     res_id: current_reply_data.res_id,
    //     parent_id: current_reply_data.parent_id,
    //     target_talk_id: current_reply_data.target_talk_id,
    //     target_user_id: current_reply_data.target_user_id,
    //     content: '回复@ ' + current_reply_data.target_talk_nickname + '：' + content
    //   }
    // } else {
    //   parData = {
    //     openid: openid,
    //     res_id: sonComment.res_id,
    //     parent_id: sonComment.id,
    //     target_talk_id: sonComment.id,
    //     target_user_id: sonComment.user_id,
    //     content: content
    //   }
    // }
    // _this.setData({
    //   parData: parData
    // })


    wx.request({
      url: 'https://shipin.lekuaiwang.com/api/talk/add',
      data: parData,
      method: 'POST',
      success: function (res) {
        // success
        console.log(res.data.data)
        // var new_comment = {
        //   res_id: _this.data.parData.res_id,
        //   parent_id: _this.data.parData.parent_id,
        //   target_talk_id: _this.data.parData.target_talk_id,
        //   target_user_id: _this.data.parData.target_user_id,
        //   content: _this.data.parData.content,
        //   target_talk_nickname: _this.data.parData.target_talk_nickname,
        //   nickname: userinfo.nickName,
        //   headimgurl: userinfo.avatarUrl,
        //   have_del: true,
        //   is_good: false,
        //   good_count: 0
        // }
        // comments.unshift(new_comment)
        _this.setData({
          wannerTalk: false,
          // 'comments_list.last': comments
        })
        wx.showToast({
          title: '回复成功'
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  toggleCheck: function (e) {
    console.log(e.currentTarget.dataset.content)
    this.setData({
      _num: e.currentTarget.dataset.num,
      _reason: e.currentTarget.dataset.content
    })
  },
  getReason: function (e) {
    var val = e.detail.value
    console.log(val)
    this.setData({
      reason_val: val
    })
  },
  exposeTap: function (e) {
    console.log(e.currentTarget.dataset)
    var dataset = e.currentTarget.dataset
    this.setData({
      isExpose: true,
      expose_talk: {
        target_user_id: dataset.talk_user_id,
        talk_id: dataset.id,
      }
    })
  },
  exposeFun: function (e) {
    var _this = this
    var openid = wx.getStorageSync('openid')
    this.setData({
      isExpose: false,
    })
    wx.request({
      url: 'https://shipin.lekuaiwang.com/api/talk/report',
      data: {
        openid: openid,
        target_user_id: _this.data.expose_talk.talk_user_id,
        talk_id: _this.data.expose_talk.talk_user_id,
        reason: _this.data.content,
        content: _this.data.reason_val
      },
      method: 'POST',
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: "举报成功"
          })
        }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })

  },
  showInp: function (e) {
    this.setData({
      inpHidden: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})